/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var search = function (nums, target) {

    let l = 0,
        r = nums.length - 1,
        mid;
    if(r===-1) {
        return -1
    }
    if(r===0){
       return  target === nums[0] ? 0 : -1
    }

    while(r>=l){
        mid = (r+l)>>1
        if(nums[mid]===target){
            return mid
        }
        if(nums[l]<=nums[mid]){
            if(nums[l]<=target&&target<nums[mid]){
                r = mid -1
            }else{
                l = mid+1
            }
        }else{
            if(nums[mid]<target&&target<=nums[r]){
                l = mid +1
            }else{
                r = mid -1
            }
        }
        
    }
    return -1
   
};


console.log(search([4, 5, 6, 7, 0, 1, 2], 4));
